
我們想要調色,首先創建一個ColorActivity,那他的xml如上圖。
設計成一個HorizontalScrollView,水平滾動視圖,是一個可以水平方向滾動的佈局容器,允許它大於物理顯示。
水平滾動視圖(HorizontalScrollView)是一個框佈局(FrameLayout),這意味著你應該將一個包含全部滾動內容的子項放進去。
該子項本身可以是一個帶有複雜對象的佈局管理器(layout manager)。

for (Colors color : Colors.values())
這是指,foreach也就是color這個變數會跑Colors這個class裡面的所有values。
再來code建button。
LinearLayout.LayoutParams layout =
new LinearLayout.LayoutParams(128, 128);
我們的大小為設置(height:128,width:128)。
layout.setMargins(6, 6, 6, 6);
邊距。
button.setId(color.parseColor());
button.setLayoutParams(layout);
button.setBackgroundColor(color.parseColor());
button.setOnClickListener(listener);
color_gallery.addView(button);
button以顏色來設置id。layout塞入button。button設計顏色。button註冊觸發事件。button加入畫面View中(linearlayout)。
寫一個class ColorListener實作View.OnClickListener。
String action = ColorActivity.this.getIntent().getAction();
if (action!=null&&action.equals("android.intent.action.CHOOSE_COLOR")) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ColorActivity.this).edit();
editor.putInt("DEFAULT_COLOR", view.getId());
editor.commit();
finish();}
action=拿到intent過來的動作。"android.intent.action.CHOOSE_COLOR",這個自己加的action是的話,繼續往下面。SharedPreferences.Editor為了存入值,key為"DEFAULT_COLOR",value則是view拿到的對應Id。commit讓SharedPreferences.Editor溝通(存放)finish()結束這個Activity。else {
Intent result = new Intent();
result.putExtra("colorId", view.getId());
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ColorActivity.this).edit();
editor.putInt("DEFAULT_COLORS", view.getId());
editor.commit();
setResult(RESULT_OK, result);
finish();}
new 一個Intent。key為colorId,value為使用者點的button對應到的Id,也就是我們剛剛設置的Id。SharedPreferences.Editor存放,key為"DEFAULT_COLORS",value一樣是view拿到的對應Id。commit讓SharedPreferences.Editor溝通(存放)finish()結束這個Activity。